<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="http://ajax-api.itheima.net/api/data" method="post">
        <input type="text" name="username" class="username" placeholder="请输入用户名">
        <br>
        <input type="text" name="food" placeholder="请输入喜欢的食物">
        <br>
        <input type="textarea" name="sign" placeholder="请输入个性签名">
        <br>
        <button type="submit">提交</button>
    </form>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="../04-源代码/00.knowledges/lib/form-serialize.js"></script>
    <script>
        // // 阻止默认行为
        // document.querySelector('button').onclick = function (e) {
        //     e.preventDefault()
        // }
        // 二选一阻止
        document.querySelector('form').onsubmit = function (e) {
            e.preventDefault()
            // 用axios发送请求

            const form = document.querySelector('form')
            const data1 =  serialize(form, {hash: true})
            console.log(data1)
            axios({
                    url: 'http://ajax-api.itheima.net/api/data',
                    method: 'post',
                    data: data1,
                })
                .then(res => console.log(res))
        }
    </script>
</body>

</html>